//
//  XHAnimateViewController.m
//  testApp
//
//  Created by mc309 on 2017/4/18.
//  Copyright © 2017年 ColdMountain. All rights reserved.
//

#import "XHAnimateViewController.h"

#import "HMSegmentedControl.h"

#import "Services.h"
#import "RLUser.h"
#import "RLMenu.h"

#import <Realm.h>

#define S_width [UIScreen mainScreen].bounds.size.width
#define S_height [UIScreen mainScreen].bounds.size.height

@interface XHAnimateViewController ()

@property (weak, nonatomic) IBOutlet UILabel *ui_label;

@property (nonatomic, strong) HMSegmentedControl *segmentedControlOne;
@property (nonatomic, strong) HMSegmentedControl *segmentedControlTwo;
@property (nonatomic, strong) HMSegmentedControl *segmentedControlThree;

@property (nonatomic, strong) NSMutableArray *firstTitle;
@property (nonatomic, strong) NSMutableArray *secondTitle;
@property (nonatomic, strong) NSMutableArray *thirdTitle;

@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic, strong) NSArray *seArray;
@property (nonatomic, strong) NSArray *thArray;

@property (nonatomic, strong) RLMRealm *rl_realm;
@property (nonatomic, strong) RLMenu *rl_menu;
@property (nonatomic, strong) RLMenus *rl_menus;
@end

@implementation XHAnimateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.firstTitle=[NSMutableArray array];
    self.secondTitle=[NSMutableArray array];
    self.thirdTitle=[NSMutableArray array];
//    NSLog(@"%@",NSStringFromClass([self class]));
//    NSLog(@"%@",NSStringFromClass([super class]));
    self.rl_realm=[RLMRealm defaultRealm];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *pathStr = paths.firstObject;
    self.rl_realm = [RLMRealm realmWithURL:[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@",pathStr,@"custom.realm"]]];
    NSLog(@"%@",[NSString stringWithFormat:@"%@/%@",pathStr,@"custom.realm"]);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)onClick:(id)sender {
//    [self.rl_realm addOrUpdateObject:RLMenu]; 
//    [RLMenu createOrUpdateInRealm:self.rl_realm withValue:@{@"city":@"曲江公馆",@"lat":@"34.195",@"lon":@"108.881"}];
    [self.rl_realm beginWriteTransaction];
    [self.rl_realm deleteAllObjects];
    [self.rl_realm commitWriteTransaction];
}
- (IBAction)searchAction:(id)sender {
   
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self getFunctionalClassification];
}

- (void)getFunctionalClassification
{
    self.rl_menus = [[RLMenus alloc]init];
    [ServiceManager getFunctionalClassificationWithSuccess:^(NSDictionary *data) {
        self.dataArray =data[@"menus"];
        NSString * str=@"";
        for (NSDictionary *dic in self.dataArray)
        {
            str=dic[@"title"];
            [self.firstTitle addObject:str];
            [self.rl_menus.menus addObject:str];
        }
        
        [self addSegmentsViewControlWithTitleArray:self.firstTitle];
    } failure:^(NSDictionary *data) {
        
    }];
}


- (void)addSegmentsViewControlWithTitleArray:(NSArray*)array
{
    HMSegmentedControl * segmentControl = [[HMSegmentedControl alloc]initWithSectionTitles:array];
    segmentControl.backgroundColor=[UIColor clearColor];
    segmentControl.frame = CGRectMake(0, 0, S_width, 45);
    segmentControl.segmentEdgeInset = UIEdgeInsetsMake(10, 0, 10, 5);
    segmentControl.segmentWidthStyle=HMSegmentedControlSegmentWidthStyleFixed;
    segmentControl.selectionStyle = HMSegmentedControlSelectionStyleArrow;
    segmentControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
    segmentControl.selectionIndicatorColor=[ColorManager colorWithHexString:@"#f2963a" alpha:1];
    segmentControl.selectionIndicatorHeight=5;
    segmentControl.verticalDividerEnabled = NO;
    //    segmentedControl1.verticalDividerColor = [UIColo/r colorWithWhite:0 alpha:0.3];
    //    segmentedControl1.verticalDividerWidth = 0.5f;
    [segmentControl setTitleFormatter:^NSAttributedString *(HMSegmentedControl *segmentedControl, NSString *title, NSUInteger index, BOOL selected) {
        NSAttributedString *attString = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName : [ColorManager colorWithHexString:@"#000000" alpha:0.9],NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Medium" size:15]}];//systemFontOfSize:14
        if (selected)
        {
            attString = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName : [ColorManager colorWithHexString:@"#f2963a" alpha:1],NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Medium" size:16]}];
        }
        return attString;
    }];
    [segmentControl addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:segmentControl];
    self.segmentedControlOne=segmentControl;
    [self segmentedControlChangedValue:segmentControl];
}

- (void)addSegmentsViewControlTwoWithTitleArray:(NSArray*)array
{
    [self.segmentedControlTwo removeFromSuperview];
    HMSegmentedControl * segmentControl = [[HMSegmentedControl alloc]initWithSectionTitles:array];
    segmentControl.backgroundColor=[UIColor clearColor];
    segmentControl.frame = CGRectMake(0, 45, S_width, 45);
    segmentControl.segmentEdgeInset = UIEdgeInsetsMake(10, 0, 10, 5);
    segmentControl.segmentWidthStyle=HMSegmentedControlSegmentWidthStyleFixed;
    segmentControl.selectionStyle = HMSegmentedControlSelectionStyleArrow;
    segmentControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationNone;
    segmentControl.selectionIndicatorColor=[ColorManager colorWithHexString:@"#f2963a" alpha:1];
    segmentControl.selectionIndicatorHeight=5;
    segmentControl.verticalDividerEnabled = NO;
    //    segmentedControl1.verticalDividerColor = [UIColo/r colorWithWhite:0 alpha:0.3];
    //    segmentedControl1.verticalDividerWidth = 0.5f;
    [segmentControl setTitleFormatter:^NSAttributedString *(HMSegmentedControl *segmentedControl, NSString *title, NSUInteger index, BOOL selected) {
        NSAttributedString *attString = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName : [ColorManager colorWithHexString:@"#000000" alpha:0.9],NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Medium" size:15]}];//systemFontOfSize:14
        if (selected) {
            attString = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName : [ColorManager colorWithHexString:@"#f2963a" alpha:1],NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Medium" size:16]}];
        }
        return attString;
    }];
    [segmentControl addTarget:self action:@selector(segmentedControlChangedValueTwo:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:segmentControl];
    self.segmentedControlTwo=segmentControl;

    [self segmentedControlChangedValueTwo:segmentControl];
}

- (void)addSegmentsViewControlThreeWithTitleArray:(NSArray*)array
{
    [self.segmentedControlThree removeFromSuperview];
    HMSegmentedControl * segmentControl = [[HMSegmentedControl alloc]initWithSectionTitles:array];
    segmentControl.backgroundColor=[UIColor clearColor];
    segmentControl.frame = CGRectMake(0, 90, S_width, 45);
    segmentControl.segmentEdgeInset = UIEdgeInsetsMake(10, 0, 10, 5);
    segmentControl.segmentWidthStyle=HMSegmentedControlSegmentWidthStyleFixed;
    segmentControl.selectionStyle = HMSegmentedControlSelectionStyleArrow;
    segmentControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationNone;
    segmentControl.selectionIndicatorColor=[ColorManager colorWithHexString:@"#f2963a" alpha:1];
    segmentControl.selectionIndicatorHeight=5;
    segmentControl.verticalDividerEnabled = NO;
    //    segmentedControl1.verticalDividerColor = [UIColo/r colorWithWhite:0 alpha:0.3];
    //    segmentedControl1.verticalDividerWidth = 0.5f;
    [segmentControl setTitleFormatter:^NSAttributedString *(HMSegmentedControl *segmentedControl, NSString *title, NSUInteger index, BOOL selected) {
        NSAttributedString *attString = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName : [ColorManager colorWithHexString:@"#000000" alpha:0.9],NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Medium" size:15]}];//systemFontOfSize:14
        if (selected) {
            attString = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName : [ColorManager colorWithHexString:@"#f2963a" alpha:1],NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Medium" size:16]}];
        }
        return attString;
    }];
    [segmentControl addTarget:self action:@selector(segmentedControlChangedValueThree:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:segmentControl];
    self.segmentedControlThree=segmentControl;
    [self segmentedControlChangedValueThree:segmentControl];
}



- (void)segmentedControlChangedValue:(HMSegmentedControl *)segmentedControl
{
    NSDictionary * dic=self.dataArray[segmentedControl.selectedSegmentIndex];
    self.seArray = dic[@"menus"];
    NSString * str=@"";
    [self.secondTitle removeAllObjects];
    for (NSDictionary *dic in self.seArray)
    {
        str=dic[@"title"];
        [self.secondTitle addObject:str];
    }
    [self addSegmentsViewControlTwoWithTitleArray:self.secondTitle];
    [PromptAction showInfoPrompt:@""];
}

- (void)segmentedControlChangedValueTwo:(HMSegmentedControl *)segmentedControl
{
    
    NSDictionary * dic=self.seArray[segmentedControl.selectedSegmentIndex];
    self.thArray=dic[@"menus"];
    if (self.thArray.count!=0)
    {
        NSString * str=@"";
        [self.thirdTitle removeAllObjects];
        for (NSDictionary *dic in self.thArray)
        {
            str=dic[@"title"];
            [self.thirdTitle addObject:str];
        }
        [self addSegmentsViewControlThreeWithTitleArray:self.thirdTitle];
    }
    else
    {
        [self.segmentedControlThree removeFromSuperview];
    }
    NSLog(@"lat:%@\nlon:%@",dic[@"lat"],dic[@"lon"]);
}

- (void)segmentedControlChangedValueThree:(HMSegmentedControl *)segmentedControl
{
    NSDictionary *dic= self.thArray[segmentedControl.selectedSegmentIndex];
    NSLog(@"lat:%@\nlon:%@",dic[@"lat"],dic[@"lon"]);
}



@end
